<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/17
 * Time: 16:08
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Carseries extends Base
{
    /**
     * 车系列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $cs_name = input('cs_name'); // 名称
            $cs_fuel = input('cs_fuel'); // 状态
            $start = input('start'); // 开始时间
            $end = input('end'); // 结束时间
            $where = [];
            if(isset($cs_fuel) && $cs_fuel != ""){
                $where['cs_fuel'] = $cs_fuel;
            }
            if(isset($cs_name) && $cs_name != ''){
                $where['cs_name'] = ['like', '%' . $cs_name . '%'];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['cs_time'] = ['>= time', $start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['cs_time'] = ['<= time', $end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['cs_time'] = ['between time', [$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('car_series')->where($where)->count();// 获取总条数
            $lists = Db::name('car_series')
                ->where($where)
                ->field('cs_id, cs_name, cs_fuel, cs_time')
                ->order('cs_time desc')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加车系
     * @return mixed|\think\response\Json
     */
    public function addSeries(){
        if(Request::instance()->isPost()){
            $cs_name = trim(input('cs_name'), ' ');
            $cs_fuel = input('cs_fuel');
            $insert = [
                'cs_name' => $cs_name,
                'cs_fuel' => $cs_fuel,
                'cs_time' => date('Y-m-d H:i:s'),
                'cs_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_series')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('carseries/addseries');
    }
    
    /**
     * 编辑车系
     * @return mixed|\think\response\Json
     */
    public function seriesEdit(){
        $cs_id = input('cs_id');
        if(Request::instance()->isPost()){
            $cs_name = trim(input('cs_name'), ' ');
            $cs_fuel = input('cs_fuel');
            $update = [
                'cs_name' => $cs_name,
                'cs_fuel' => $cs_fuel,
                'cs_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_series')->where('cs_id', $cs_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_car_series')->where('cs_id', $cs_id)->find();
        $this->assign([
            'cs_id' => $cs_id,
            'info' => $info
        ]);
        return $this->fetch('carseries/seriesedit');
    }
    
    /**
     * 删除车系
     * @return \think\response\Json
     */
    public function seriesDel(){
        $cs_id = input('id');
        // 是否有经销商属于该车系
        $have_dealer = Db::table('up_dealer')->where('dealer_car_id', $cs_id)->find();
        // 是否有试乘试驾意向车系属于该车系
        $have_drive = Db::table('up_drive')->where('try_car_id', $cs_id)->find();
        if($have_dealer || $have_drive){
            return json(['code' => 100, 'msg' => '该车系已被使用']);
        }
        $res = Db::table('up_car_series')->delete($cs_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}